﻿# -*- coding: utf-8 -*-

class WaveCatalog:
  def __init__(self, tags):
    self.annotation = []
    self.wave_catalog = [] #sorted list of ComparableWaves
    self.tags = tags #tag hierarchy
       
  def put(self, cmp_wave):
    """ Puts ComparableWave to list and sorts this list. """
    self.wave_catalog.append(cmp_wave)
    self.wave_catalog.sort()
    
  def remove(self, wave_id):
    """ Removes all [ComparableWave]s which have the same wave_id. """
    indexes = range(len(self.wave_catalog))
    indexes.reverse()
    for i in indexes:
      if self.wave_catalog[i].wave_id == wave_id:
        del self.wave_catalog[i]
        
  def get(self, wave_id):
    """returns ComparableWave."""
    i = 0
    while i < len(self.wave_catalog):
      if self.wave_catalog[i].wave_id == wave_id:
        return self.wave_catalog[i]
      i += 1        
  
  def __unicode__(self):
    """ Returns unicode string and fills annotation list in format: 
      {"key": key, "value": self.wave_catalog[i].wave_id, "range": {"begin": begin, "end": end} } for each cmp_wave. """
    key, tab, result_str = "link/wave", u"        ", u"\n"
    i = 0
    print_titles = [ True for q in range(len(self.tags))] #0 - title must be printed. And all titles after 0 must be printed

    while i < len(self.wave_catalog):
      header = u""      
      j = 0 #title must be printed between 1.1 + 3.1 and 1.1 + 2.2, lvl2 
      if i > 0:
        while (j < self.wave_catalog[i].end + 1) and self.wave_catalog[i].tag_indexes[j] == self.wave_catalog[i - 1].tag_indexes[j]:
          j += 1 #length of missing tags. these tags have been printed       
        
      for k in range(j, self.wave_catalog[i].end + 1): #printing tags, k means a shift for current tag and place in tag's levels
        #logging.info("self.tags: %s, self.wave_catalog[i].tag_indexes: %s" % (self.tags, self.wave_catalog[i].tag_indexes))
        if self.wave_catalog[i].tag_indexes[k] < len(self.tags[k]["elements"]):
          if print_titles[k]: #do we need level's title:  
            header += k * tab + self.tags[k]["category"] + "\n"
            print_titles[k] = False #we have printed
          header += k * tab  + u"  " + self.tags[k]["elements"][self.wave_catalog[i].tag_indexes[k]] + u"\n" #ppc        
      result_str += header
      
      result_str += self.wave_catalog[i].end * tab + "    " 
      begin = len(result_str) + 1
      result_str += unicode(self.wave_catalog[i]) + "\n" #there is at least one wave wave in this category
      end = len(result_str) 
      self.annotation.append({"key": key, "value": self.wave_catalog[i].wave_id, "range": {"begin": begin, "end": end} })
      i += 1
      while i < len(self.wave_catalog) and self.wave_catalog[i - 1] == self.wave_catalog[i]: #wave's output
        result_str += self.wave_catalog[i].end * tab + "    "
        begin = len(result_str) + 1
        result_str += unicode(self.wave_catalog[i]) + "\n"
        end = len(result_str) 
        self.annotation.append({"key": key, "value": self.wave_catalog[i].wave_id, "range": {"begin": begin, "end": end} })
        i+=1
      #(i-1)wave has been added, i - is next wave
      
      #correcting future title output:
      if i < len(self.wave_catalog):
        if self.wave_catalog[i].end < self.wave_catalog[i - 1].end:
          for q in range(self.wave_catalog[i].end + 1, len(self.tags)):
            print_titles[q] = True
        if self.wave_catalog[i - 1].begin < self.wave_catalog[i].begin:
          print_titles = [ True for q in range(len(self.tags))] #must be reseted between levels
          result_str += "-" * 30 + "\n"
      
    return result_str